package com.yanqu.road.dao.impl.curio;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.curio.config.CurioBadgeInfo;
import com.yanqu.road.entity.curio.config.CurioItemInfo;
import com.yanqu.road.entity.curio.config.CurioItemSuitInfo;
import com.yanqu.road.entity.curio.config.CurioPrivilegeDay;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CurioConfigDaoImpl extends TempDao {

    private static String getCurioItemInfoSql = "select * from `t_p_curio` ORDER BY `id` ASC";
    public Map<Integer, CurioItemInfo> getCurioItemInfo() {
        Map<Integer, CurioItemInfo> map = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getCurioItemInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CurioItemInfo curioItemInfo = new CurioItemInfo();
                curioItemInfo.setId(rs.getInt("id"));
                curioItemInfo.setQuality(rs.getInt("quality"));
                curioItemInfo.setBaseSkill(rs.getInt("baseSkill"));
                curioItemInfo.setBaseSkillFixedAdd(StringUtils.stringToIntegerList(rs.getString("baseSkillFixedAdd"), "\\|"));
                curioItemInfo.setSpecialSkillCond(rs.getString("specialSkillCond"));
                if(!"0".equals(curioItemInfo.getSpecialSkillCond())){
                    List<String> paramsList = StringUtils.stringToStringList(curioItemInfo.getSpecialSkillCond(), "=");
                    if(paramsList.size() == 2){
                        int type = Integer.parseInt(paramsList.get(0));
                        List<Integer> paramValues = StringUtils.stringToIntegerList(paramsList.get(1), ";");
                        curioItemInfo.setType(type);
                        curioItemInfo.setParamList(paramValues);
                    }
                }
                curioItemInfo.setSpecialSkill(rs.getInt("specialSkill"));
                curioItemInfo.setIsFragmentLottery(rs.getInt("isFragmentLottery"));
                curioItemInfo.setIsWholeCardLottery(rs.getInt("isWholeCardLottery"));
                curioItemInfo.setOverflowChange(rs.getInt("overflowChange"));
                curioItemInfo.setSpecialSkillCondMax(rs.getString("specialSkillCondMax"));

                curioItemInfo.setPrivilege(rs.getString("privilege"));
                if(!"0".equals(curioItemInfo.getPrivilege()) && !StringUtils.isNullOrEmpty(curioItemInfo.getPrivilege())){
                    List<String> paramsList = StringUtils.stringToStringList(curioItemInfo.getPrivilege(), "=");
                    if(paramsList.size() > 1){
                        int type = Integer.parseInt(paramsList.get(0));
                        List<String> paramValues = StringUtils.stringToStringList(paramsList.get(1), "\\|");
                        curioItemInfo.getPrivilegeDayMap().put(type, new ArrayList<>());
                        for (String paramValue : paramValues) {
                            List<Integer> valueList = StringUtils.stringToIntegerList(paramValue, ";");
                            if(valueList.size() == 2) {
                                CurioPrivilegeDay privilegeDay = new CurioPrivilegeDay();
                                privilegeDay.setStar(valueList.get(0));
                                privilegeDay.setAddDay(valueList.get(1));
                                curioItemInfo.getPrivilegeDayMap().get(type).add(privilegeDay);
                            }
                        }
                    }
                }
                curioItemInfo.setStarUpgradeType(rs.getInt("starUpgradeType"));
                map.put(curioItemInfo.getId(), curioItemInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getCurioItemSuitInfoSql = "select * from `t_p_curio_suit` ORDER BY `id` ASC";
    public Map<Integer, CurioItemSuitInfo> getCurioItemSuitInfo() {
        Map<Integer, CurioItemSuitInfo> result = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getCurioItemSuitInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CurioItemSuitInfo curioItemSuitInfo = new CurioItemSuitInfo();
                curioItemSuitInfo.setId(rs.getInt("id"));
                curioItemSuitInfo.setQuality(rs.getInt("quality"));
                curioItemSuitInfo.setCurioList(StringUtils.stringToIntegerList(rs.getString("curioList"), ";"));
                curioItemSuitInfo.setSkill(rs.getInt("skill"));
                curioItemSuitInfo.setSkillUpgradeCostType(rs.getInt("skillUpgradeCostType"));
                curioItemSuitInfo.setExtraSkill(rs.getString("extraSkill"));
                result.put(curioItemSuitInfo.getId(), curioItemSuitInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }


    private static String getCurioBadgeInfoSql = "select * from `t_p_badge` where `type`= 8 ORDER BY `id` ASC";
    public List<CurioBadgeInfo> getCurioBadgeInfoList() {
        List<CurioBadgeInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getCurioBadgeInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CurioBadgeInfo curioBadgeInfo = new CurioBadgeInfo();
                curioBadgeInfo.setId(rs.getInt("id"));
                curioBadgeInfo.setLevel(rs.getInt("level"));
                curioBadgeInfo.setUnlockValue(rs.getLong("unlockValue"));
                curioBadgeInfo.setSkillsList(StringUtils.stringToIntegerList(rs.getString("skillsList"), "\\|"));
                curioBadgeInfo.setSpecialEffect(rs.getString("specialEffect"));
                curioBadgeInfo.setExParam(rs.getString("exParam"));
                list.add(curioBadgeInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
